package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项controller
 *
 * @author likeWen
 * @since 2022/11/19 11:41
 */
@RestController
@Slf4j
@RequestMapping("/checkgroup")

public class CheckgroupController {
    @Autowired
    private CheckgroupService checkgroupService;
    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查项-分页查询]data:{}", queryPageBean);

        //rpc查询数据
        PageResult pageResult = checkgroupService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }
    /**
     * 检查组新增数据
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("[检查组-新增数据]data:{},ids:{}", checkGroup,checkitemIds);
        checkgroupService.add(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }
    /**
     * 编辑检查组，检查组数据回显
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("[检查组-根据ID查询]id:{}", id);
        CheckGroup checkGroup = checkgroupService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
    }
    /**
     * 编辑检查组，检查项数据回显
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.info("[检查组-根据检查项ID查询]id:{}", id);
        List<Integer> list = checkgroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,list);
    }
    /**
     * 编辑检查组，编辑实现
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("[检查组-编辑检查组]data:{},checkitemIds:{}", checkGroup,checkitemIds);
        if (checkitemIds == null || checkGroup == null){
            return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }else {
            checkgroupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
        }
    }
    /**
     * 删除检查组
     */
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(Integer id){
        log.info("[检查组-根据ID删除]id:{}", id);
        Boolean Aboolean = checkgroupService.deleteById(id);
        if (Aboolean){
            return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
        }else {
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 新建套餐-查询检查组
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("[套餐新建-检查项-查询]data:{}");
        List<CheckGroup> list = checkgroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }
}
